package com.jd.jr.insights.dm.dim.dao.impl;


import com.jd.jr.insights.dm.dim.dao.DimTransformDao;
import com.jd.jr.insights.dm.dim.dao.model.JfDimBiz;
import com.jd.jr.insights.dm.dim.dao.model.JfDimTransform;
import com.jd.jr.insights.dm.dim.domain.Business;
import com.jd.jr.insights.dm.dim.domain.DimTransform;
import com.jd.jr.insights.dm.dim.util.LOG;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.ITreeNode;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.Result;
import com.jd.jr.insights.dm.dim.util.easyui.paginate.Pagger;
import com.jd.jr.insights.dm.dim.util.easyui.paginate.PaggerForEasyUI;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by guoxuefei1 on 2017/7/18.
 */
public class DimTransformDaoImpl implements DimTransformDao {
    public List<DimTransform> getList() {
        List<JfDimTransform> dimTransforms= JfDimTransform.dao.find("select * from jf_dim_transform where is_valid=1");


        List<DimTransform> dimTransformList=new ArrayList<DimTransform>(dimTransforms.size());

        Iterator<JfDimTransform> inter=dimTransforms.iterator();
        while(inter.hasNext()){
            JfDimTransform dimTransform=inter.next();

            JfDimBiz _jfDimBiz =dimTransform.getJfDimBiz();

            LOG.INFO(DimTransformDaoImpl.class.toString(),"定位"+dimTransform.getBizId()+""+ dimTransform.getOldTableNm());

            LOG.INFO(DimTransformDaoImpl.class.toString(),_jfDimBiz.getValue());


            Business _biz=new Business(_jfDimBiz.getBizId(),_jfDimBiz.getCode(),_jfDimBiz.getValue());





            DimTransform dimTransform1=new DimTransform(dimTransform.getId(),
                    dimTransform.getOldTableNm(),
                    dimTransform.getOldColumnNm1(),
                    dimTransform.getOldKeyId1(),
                    dimTransform.getOldKeyNm1(),
                    dimTransform.getNewTableNm(),
                    dimTransform.getNewColumnNm(),
                    dimTransform.getNewKeyId(),
                    dimTransform.getNewKeyNm(),
                    dimTransform.getIsValid(),
                    dimTransform.getCreatedTime(),
                    dimTransform.getUpdatedTime(),
                    _biz
            );
            dimTransformList.add(dimTransform1);
        }

        return dimTransformList;
    }


    public int getTotalRows(){
        return Db.queryInt("select count(1) from jf_dim_transform where is_valid=1");

    }
    public Pagger getListInPage(int pageNum) {


        Page page= JfDimTransform.dao.paginate(pageNum, Pagger.PAGE_SIZE,"select * ","from jf_dim_transform where is_valid=1 and 1=? order by old_table_nm,old_column_nm1,old_key_id1,new_table_nm,new_column_nm,new_key_id,id",1);


        List<DimTransform> dimTransformList=new ArrayList<DimTransform>(page.getList().size());

        Iterator<JfDimTransform> inter=page.getList().iterator();
        while(inter.hasNext()) {
            JfDimTransform dimTransform = inter.next();

            JfDimBiz _jfDimBiz = dimTransform.getJfDimBiz();

            Business _biz = new Business(_jfDimBiz.getBizId(), _jfDimBiz.getCode(), _jfDimBiz.getValue());


            DimTransform dimTransform1 = new DimTransform(dimTransform.getId(),
                    dimTransform.getOldTableNm(),
                    dimTransform.getOldColumnNm1(),
                    dimTransform.getOldKeyId1(),
                    dimTransform.getOldKeyNm1(),
                    dimTransform.getNewTableNm(),
                    dimTransform.getNewColumnNm(),
                    dimTransform.getNewKeyId(),
                    dimTransform.getNewKeyNm(),
                    dimTransform.getIsValid(),
                    dimTransform.getCreatedTime(),
                    dimTransform.getUpdatedTime(),
                    _biz
            );
            dimTransformList.add(dimTransform1);
        }
        return  new PaggerForEasyUI(page.getTotalRow(),page.getTotalPage(),pageNum,dimTransformList);
    }

    public Pagger getListInPage(int pageNum, DimTransform dimTransforms) {

        StringBuffer  sqlFilter=new StringBuffer();
        if(dimTransforms.getId()!=null){
            sqlFilter.append("and  id=").append(dimTransforms.getId());
        }
        if(dimTransforms.getOld_table_nm()!=null){
            sqlFilter.append("and  old_table_nm='").append(dimTransforms.getOld_table_nm()).append("'");
        }
        if(dimTransforms.getOld_column_nm1()!=null){
            sqlFilter.append("and  old_column_nm1='").append(dimTransforms.getOld_column_nm1()).append("'");
        }
        if(dimTransforms.getOld_key_id1()!=null){
            sqlFilter.append("and old_key_id1='").append(dimTransforms.getOld_key_id1()).append("'");
        }
        if(dimTransforms.getOld_key_nm1()!=null){
            sqlFilter.append("and old_key_nm1='").append(dimTransforms.getOld_key_nm1()).append("'");
        }
        if(dimTransforms.getNew_table_nm()!=null){
            sqlFilter.append("and new_table_nm='").append(dimTransforms.getNew_table_nm()).append("'");
        }
        if(dimTransforms.getNew_column_nm()!=null){
            sqlFilter.append("and new_column_nm='").append(dimTransforms.getNew_column_nm()).append("'");
        }
        if(dimTransforms.getNew_key_id()!=null){
            sqlFilter.append("and new_key_id='").append(dimTransforms.getNew_key_id()).append("'");
        }
        if(dimTransforms.getNew_key_nm()!=null){
            sqlFilter.append("and new_key_nm='").append(dimTransforms.getNew_key_nm()).append("'");
        }
        if(dimTransforms.getBiz()!=null){
            sqlFilter.append("and biz_id=").append(dimTransforms.getBiz().getBiz_id());
        }


        Page page= JfDimTransform.dao.paginate(pageNum, Pagger.PAGE_SIZE,"select * ","from jf_dim_transform where is_valid=1 and 1=? "+sqlFilter+"  order by old_table_nm,old_column_nm1,old_key_id1,new_table_nm,new_column_nm,new_key_id,id",1);


        List<DimTransform> dimTransformList=new ArrayList<DimTransform>(page.getList().size());

        Iterator<JfDimTransform> inter=page.getList().iterator();
        while(inter.hasNext()) {
            JfDimTransform jfDimTransform = inter.next();

            JfDimBiz _jfDimBiz = jfDimTransform.getJfDimBiz();

            Business _biz = new Business(_jfDimBiz.getBizId(), _jfDimBiz.getCode(), _jfDimBiz.getValue());


            DimTransform dimTransform1 = new DimTransform(jfDimTransform.getId(),
                    jfDimTransform.getOldTableNm(),
                    jfDimTransform.getOldColumnNm1(),
                    jfDimTransform.getOldKeyId1(),
                    jfDimTransform.getOldKeyNm1(),
                    jfDimTransform.getNewTableNm(),
                    jfDimTransform.getNewColumnNm(),
                    jfDimTransform.getNewKeyId(),
                    jfDimTransform.getNewKeyNm(),
                    jfDimTransform.getIsValid(),
                    jfDimTransform.getCreatedTime(),
                    jfDimTransform.getUpdatedTime(),
                    _biz
            );
            dimTransformList.add(dimTransform1);
        }
        return  new PaggerForEasyUI(page.getTotalRow(),page.getTotalPage(),pageNum,dimTransformList);
    }

    public Boolean deleteById(DimTransform dimTransform) {
        JfDimTransform daoa=JfDimTransform.dao.findById(dimTransform.getId());
        daoa.setIsValid(0);
        daoa.setUpdatedTime(new Date());
        Boolean result=daoa.update();
        return result;
    }

    public Boolean deleteByIds(Integer[] ids) {

        int count= Db.update("update jf_dim_transform set is_valid=0 where id in ("+StringUtils.join(ids,",")+");");

        if(count>0){
            return true;
        }
        return false;
    }

    public Boolean update(DimTransform dimTransform) {
        JfDimTransform daoa=JfDimTransform.dao.findById(dimTransform.getId());
        daoa.setOldTableNm(dimTransform.getOld_table_nm());
        daoa.setOldColumnNm1(dimTransform.getOld_column_nm1());
        daoa.setOldKeyId1(dimTransform.getOld_key_id1());
        daoa.setOldKeyNm1(dimTransform.getOld_key_nm1());
        daoa.setNewTableNm(dimTransform.getNew_table_nm());
        daoa.setNewColumnNm(dimTransform.getNew_column_nm());
        daoa.setNewKeyId(dimTransform.getNew_key_id());
        daoa.setNewKeyNm(dimTransform.getNew_key_nm());
        daoa.setBizId(dimTransform.getBiz().getBiz_id());
        daoa.setUpdatedTime(new Date());
        Boolean result=daoa.update();


        return result;
    }

    public Boolean save(DimTransform dimTransform) {
        JfDimTransform daoa=new  JfDimTransform();
        daoa.setOldTableNm(dimTransform.getOld_table_nm());
        daoa.setOldColumnNm1(dimTransform.getOld_column_nm1());
        daoa.setOldKeyId1(dimTransform.getOld_key_id1());
        daoa.setOldKeyNm1(dimTransform.getOld_key_nm1());
        daoa.setNewTableNm(dimTransform.getNew_table_nm());
        daoa.setNewColumnNm(dimTransform.getNew_column_nm());
        daoa.setNewKeyId(dimTransform.getNew_key_id());
        daoa.setNewKeyNm(dimTransform.getNew_key_nm());
        daoa.setIsValid(1);

        daoa.setCreatedTime(new Date());
        daoa.setUpdatedTime(new Date());
        daoa.setBizId(dimTransform.getBiz().getBiz_id());
        
        Boolean result=daoa.save();

        return result;
    }

    public List<Result> getOldTableNameList() {
        List<Record> record=Db.find("select old_table_nm from  jf_dim_transform where is_valid=1 group by old_table_nm");
        List<Result> list=new ArrayList<Result>();
        Iterator<Record> iterator=record.iterator();
        while (iterator.hasNext()){
            String str=iterator.next().getStr("old_table_nm");
            list.add(new Result(0,str));
        }
        return list;
    }

    public List<Result> getNewTableNameList() {
        List<Record> record=Db.find("select new_table_nm from  jf_dim_transform where is_valid=1 group by new_table_nm");
        List<Result> list=new ArrayList<Result>();
        Iterator<Record> iterator=record.iterator();
        while (iterator.hasNext()){
            String str=iterator.next().getStr("new_table_nm");
            list.add(new Result(0,str));
        }
        return list;
    }

    public List<Result> getNewColumnNameList() {
        List<Record> record=Db.find("select new_column_nm from  jf_dim_transform where is_valid=1 group by new_column_nm");
        List<Result> list=new ArrayList<Result>();
        Iterator<Record> iterator=record.iterator();
        while (iterator.hasNext()){
            String str=iterator.next().getStr("new_column_nm");
            list.add(new Result(0,str));
        }
        return list;
    }

    public List<Result> getOldKeyIdList() {
        List<Record> record=Db.find("select new_key_id from  jf_dim_transform where is_valid=1 group by new_key_id");
        List<Result> list=new ArrayList<Result>();
        Iterator<Record> iterator=record.iterator();
        while (iterator.hasNext()){
            String str=iterator.next().getStr("new_key_id");
            list.add(new Result(0,str));
        }
        return list;
    }

    public List<Result> getOldKeyNameList() {
         List<Record> record=Db.find("select new_key_nm from  jf_dim_transform where is_valid=1 group by new_key_nm");
        List<Result> list=new ArrayList<Result>();
        Iterator<Record> iterator=record.iterator();
        while (iterator.hasNext()){
            String str=iterator.next().getStr("new_key_nm");
            list.add(new Result(0,str));
        }
        return list;
    }

    public List<ITreeNode> getBizList() {
        List<Record> records=Db.find("select t1.biz_id ,t2.code,t2.value from (select biz_id from  jf_dim_transform  where is_valid=1 group by biz_id) t1\n" +
                "left join (select biz_id,code,value from jf_dim_biz where is_valid=1) t2 on t1.biz_id=t2.biz_id \n" +
                "group by t1.biz_id ,t2.code,t2.value;");

        List<ITreeNode> bizs=new ArrayList<ITreeNode>(records.size());
        Iterator<Record> inter=records.iterator();
        while(inter.hasNext()){
            Record bizM=inter.next();
            Business biz=new Business (bizM.getInt("biz_id")
                    ,bizM.getStr("code")
                    ,bizM.getStr("value")
                    ,null
                    ,null
                    ,null
                    ,null
                    ,null
                    ,null
            );
            bizs.add(biz);
        }

        return bizs;
    }
}
